﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WoodAdmin.Common.Helper;
using WoodAdmin.Common.Helper.Log;
using WoodAdmin.Models;
using WoodAdmin.Services;

namespace WoodAdmin.Controllers.Api
{
    //JWT的验证功能开启 Swagger中调用时，在权限验证框中输入 Bearer +登录返回的Token(注意Bearer后边有个空格)
    //如果使用Postman，则在Header中加入Key:Authorize Value:Bearer +登录返回的Token(注意Bearer后边有个空格)
    [Authorize]
    public class HomeController : BaseApiController
    {
        private readonly UserInfoService _service;

        public HomeController(UserInfoService service)
        {
            _service = service;
        }

        /// <summary>
        /// 示例Demo
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async void Index()
        {
            #region 常用参数

            //当前用户的id
            var userid = CurrentUserId;

            //获取API请求中的浏览器语言（用于国际化）
            var lang = CurrentUserLang;

            //全局变量
            GlobalHelper.SetVerify("123");
            var verify = GlobalHelper.CurrentVerify();

            //获取数据库实例
            var db = DbContext.GetIntance();
            #endregion

            #region 日志
            NLogger.Debug("Exception Message...");
            NLogger.Info("Information");
            //还有别的...
            #endregion

            #region 查询
            //查询集合，返回ISugarQueryable集合对象
            var sugarQuery = _service.Queryable();

            //查询集合，返回List集合
            List<UserInfo> list = _service.QueryableList();

            //条件查询集合，返回ISugarQueryable集合对象
            var sugarQueryif = _service.Queryable(a => a.UserID == 200565);

            //条件查询集合，返回List集合
            List<UserInfo> list2 = _service.QueryableList(a => a.UserID == 200565);

            //异步查询集合
            List<UserInfo> asyLis = await _service.QueryableAsync();

            //总条数
            int count = _service.QueryableCount(a => a.UserID == 200565);

            //是否存在
            bool exist = _service.QueryableExist(a => a.UserID == 200565);

            //单条
            UserInfo userInfo = _service.FindEntity(1);

            //异步查询单条
            UserInfo userInfo2 = await _service.FindEntityAsync(a => a.UserID == 200565);
            //还有别的...
            #endregion

            #region 增加
            UserInfo demoUser = new UserInfo();
            //插入
            _service.Insert(demoUser);
            //插入并返回ID
            var id = _service.InsertReturnIdentity(demoUser);
            //还有别的...
            #endregion

            #region 修改
            UserInfo updateUser = _service.FindEntity(200565);
            bool result = _service.Update(updateUser);
            bool result2 = await _service.UpdateAsync(updateUser);
            //还有别的
            #endregion

            #region 删除
            UserInfo user = new UserInfo();
            bool rs1 = _service.Delete(user);
            bool rs2 = await _service.DeleteAsync(user);
            bool rs3 = _service.Delete(1);
            bool rs4 = _service.Delete(a => a.UserID == 1);
            //还有别的...
            #endregion

            #region 如果以上方法不够用，请到对应Service中直接使用Db进行操作/或者直接用DB从Ctroller里操作
            //示例
            _service.DemoTest();
            var userInfoList = Db.Queryable<UserInfo>();
            #endregion
        }

        /// <summary>
        /// 示例绕过Token验证
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public string TestIngoreAuthorize()
        {
            return "Success";
        }
    }
}